

var AddrTip = Class.create({
  
  initialize: function() {
    this.firsttime = null;
    this.lasttime = null;
    this.timeout = -1;
    this.cityItemsNum = 12;
    this.currHighlightId = -1;
    this.highlightColor = "#99CCFF";
    this.selectedColor = "#77DDFF";
    this.commonColor = "#FFFFFF";
    this.maxIndex = -1;
    this.minIndex = -1;
    this.childs = this.getAddrArr();
    this.queryStr = null;
    this.type = null;
  },
  
  initEvent: function() {
    var me = this;
    jQuery.each(this.childs, function(index, ele) {
      
      jQuery(ele).mouseover(function(){
        var display = this.style.display;
        if(display == 'block') {
          this.style.backgroundColor = me.selectedColor;
          me.currHighlightId = index;
        }
      });
      
      jQuery(ele).mouseout(function(){
        var display = this.style.display;
        if(display == 'block') {
          this.style.backgroundColor = me.commonColor;
        }
      });
      
      jQuery(ele).click(function(objEvent){
        me._fillTextbox(this);
        me.currHighlightId = index;
      });
    });
    
    var objTextbox = $(this.type + "Id");
    jQuery(objTextbox).bind('blur', function(){
      me.closeTip();
    });
    
    jQuery("#address_list").mouseenter(function(){
      jQuery(objTextbox).unbind("blur");
    });
    jQuery("#address_list").mouseleave(function(){
      jQuery(objTextbox).bind("blur", function(){
        me.closeTip();
      });
    });
  },
  
  _fillTextbox: function(selAdd) {
    var re = this._getValue(selAdd);
    $(this.type + "Id").value = re.name;
    $(this.type + "CodeId").value = re.value;
    
    this.closeTip();
  },
  
  showTip: function(event, type) {
    this.type = type;
    
    var keyCode  = event.keyCode;
    if(keyCode == 38 || keyCode == 40) {
      // move up and down to select
      var up = (keyCode == 38 ? true : false);
      this.moveByArrow(up);
      return;
    }
    
    if(keyCode == 13) {
      // enter the value
      if(this.currHighlightId >= this.minIndex && this.currHighlightId <= this.maxIndex) {
        var node = this.childs[this.currHighlightId];
        this._fillTextbox(node);
      }
      return;
    }
    
    var now = new Date();
    if(!this.firsttime) {
      this.firsttime = now;
      this.lasttime = now;
    } else {
      this.firsttime = this.lasttime;
      this.lasttime = now;
    }
    if(this.lasttime - this.firsttime < 1000) {
      clearTimeout(this.timeout);
      var me = this;
      this.timeout = setTimeout(function(){
        
        var now2 = new Date();
        var cha = now2 - me.lasttime;
        //console.log("now2: " + me._outputTime(now2) + "; lasttime:" + me._outputTime(me.lasttime) + "; cha:" + cha);
        if(cha > 990) {
          //console.info("trigger");
          me.firsttime = null;
          me.lasttime = null;
          
          me.popupTip();
        } else {
        }
      }, 1000);
    }
  }, 
  
  closeTip: function() {
    if(this.currHighlightId != -1) {
      this.childs[this.currHighlightId].style.backgroundColor = this.commonColor;
    }
    
    this.firsttime = null;
    this.lasttime = null;
    this.currHighlightId = -1;
    this.minIndex = -1;
    this.maxIndex = -1;
    
    $("tuna_address").style.display = "none";
    
    // unbind the event listener
    var objTextbox = $(this.type + "Id");
    jQuery(objTextbox).unbind("blur");
    jQuery.each(this.childs, function(index, ele) {
      jQuery(ele).unbind("mouseover");
      jQuery(ele).unbind("mouseout");
      jQuery(ele).unbind("click");
    });
  }, 
  
  moveByArrow: function(up) {
    var childs = this.getAddrArr();
    if(!up) {
      this.currHighlightId ++;
      if(this.currHighlightId > this.maxIndex) {
        this.currHighlightId = this.currHighlightId - this.maxIndex - 1;
      }
    } else {
      this.currHighlightId --;
      if(this.currHighlightId < 0) {
        this.currHighlightId = this.maxIndex;
      }
    }
    
    if(up) {
      var prevId = this.currHighlightId + 1;
      if(prevId > this.maxIndex) {
        prevId = 0;
      }
      childs[prevId].style.backgroundColor = this.commonColor;
      childs[this.currHighlightId].style.backgroundColor = this.highlightColor;
    } else {
      var prevId = this.currHighlightId - 1;
      if(prevId < 0) {
        prevId = this.maxIndex;
      }
      childs[prevId].style.backgroundColor = this.commonColor;
      childs[this.currHighlightId].style.backgroundColor = this.highlightColor;
    }
  }, 
  
  popupTip: function(type) {
    if(type) {
      this.type = type;
    }
    
    this.initEvent();
    var inputCtl = $(this.type + "Id");
    var inputText = inputCtl.value.toUpperCase();
    
    if(!this.queryStr) {
      this._getResponse(inputText, inputCtl);
    } else {
      if(this.queryStr.trim() != "" && inputText.indexOf(this.queryStr) == 0) {
        // the data is the sub set of current data
        var data = this._filterData(inputText);
        this.airport = data;
        this.popupData(data, inputText);
      } else {
        this._getResponse(inputText, inputCtl);
      }
    }
    this.queryStr = inputText;
  }, 
  
  _filterData: function(inputText) {
    var re = [];
    if(this.airport) {
      var size = this.airport.length;
      for(var i = 0; i < size; i ++) {
        var v = this.airport[i];
        if((v.code && v.code.indexOf(inputText) == 0) || (v.city && v.city.indexOf(inputText))) {
          re.push(v);
        }
      }
    }
    return re;
  }, 
  
  _getResponse: function(query, inputCtl) {
    var url = "flightQueryAction!queryThreecode.action?query=" + query + "&r=" + getRandomString();
	//alert("url is:" + url);
	var me = this;
	new Ajax.Request(url, {
		method: "get",
		onComplete: function(transport) {
			var ret = transport.responseText.trim();
			//alert("get displayContinent response text is:" + ret);
			var aa = ret.split("|");
			me.airport = [];
			for(var i = 0; i < aa.length; i ++) {
			  var ae = aa[i];
			  if(ae.trim() != "") {
			    var aea = ae.split(":");
			    me.airport.push({code: aea[0], city: aea[1], country: aea[2]});
			  }
			}
			me.visibleAndMoveTip(inputCtl)
			me.popupData(me.airport.slice(0, me.cityItemsNum), inputCtl.value.toUpperCase());
		}
	});
  }, 
  
  visibleAndMoveTip: function(inputCtl) {
    var blkTip = $("tuna_address");
    blkTip.style.left = inputCtl.offsetLeft + "px";
    blkTip.style.top = (inputCtl.offsetTop + inputCtl.clientHeight) + "px";
    blkTip.style.display = "block";
  }, 
  
  popupData: function(showData, inputText) {
    var me = this;
    var bHighlight = false;
    this.getAddrArr().html(function(index){
      if(showData[index]) {
        this.style.display = "block";
        if(inputText && inputText.trim() != "" && !bHighlight && (inputText == showData[index].code || inputText == showData[index].city)) {
          this.style.backgroundColor = me.highlightColor;
          me.currHighlightId = index;
          bHighlight = true;
        }
        return "<span>" + showData[index].code + "</span>" + showData[index].city;
      } else {
        this.style.display = "none";
        this.style.backgroundColor = this.commonColor;
      }
    });
    this.maxIndex = showData.length - 1;
    if(this.maxIndex >= 0) {
      minIndex = 0;
    }
  }, 
  
  _getValue: function(node) {
    return {value: node.childNodes[0].innerHTML, name: node.childNodes[1].nodeValue};
  }, 
  
  getAddrArr: function() {
    /*var addrList = $("address_list");
    var childs = addrList.childNodes;
    var re = [];
    for(var i = 0; i < childs.length; i ++) {
      if(childs[i].nodeType == 1) re.push(childs[i]);
    }
    return re;*/
    return jQuery("#address_list a");
  }, 
  
  _outputTime: function(date) {
    return date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds();
  }
  
});

